// pages/mediaHome/mediaHome.js
let http = require('../../utils/api.js');
import {
    myHttps,
    login,
    toData
} from '../../utils/util.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hiddenLoading: false,
        list: [],
        size: 10, //页容量
        page: 1, //当前页
        mediaId:'',//媒体id
        mediaDetails:'',//媒体详情
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(e) {
        // console.log(e.id)
        this.setData({
            mediaId: e.id
        })
        this.mediaEssays(e.id,this.data.page,this.data.size)//文章
        this.mediaDetails(e.id)//媒体详情
    },
    // 媒体详情
    mediaDetails (id) {
        let url = http.api.mediaDetails + id
        var that = this;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            // console.log(res)
            if (res.code == 1) {
                this.setData({
                    mediaDetails:res.data
                })
            }
        }).catch(err => {})
    },
    // 关注媒体
    mediaFollow(e) {
        var that = this
        // console.log(e.currentTarget.dataset.id)
        var id = e.currentTarget.dataset.id
        let url = http.api.mediaFollow + id + '/fav';
        myHttps({
            url: url,
            method: "POST"
        }).then(res => {
            if (res.code == 1) {
                that.data.mediaDetails.followed = true
                that.setData({
                    mediaDetails: that.data.mediaDetails,
                    hiddenLoading: true
                })
            }
        }).catch(err => { })
    },
    // 取消关注媒体
    canclegz(e) {
        var that = this
        // console.log(e.currentTarget.dataset.id)
        let id = e.currentTarget.dataset.id
        let url = http.api.mediaFollow + id + '/fav';
        myHttps({
            url: url,
            method: "DELETE"
        }).then(res => {
            if (res.code == 1) {
                that.data.mediaDetails.followed = false
                that.setData({
                    mediaDetails: that.data.mediaDetails,
                    hiddenLoading: true
                })
            }
        }).catch(err => { })
    },
    //下拉刷新从新初始化页码,条数
    onPullDownRefresh: function () {
        //下拉三个点消失
        wx.stopPullDownRefresh();
        this.setData({
            List: [],
            size: 10,
            page: 1
        })
        this.mediaEssays(this.data.mediaId, this.data.page, this.data.size)//文章
    },
    // 上拉加载数据
    onReachBottom: function () {
        this.setData({
            size: 10,
            page: this.data.page + 1,
        })
        this.mediaEssays(this.data.mediaId, this.data.page, this.data.size)//文章
    },
    // 文章列表
    mediaEssays (id,page,size) {
        let url = http.api.mediaEssays + id + '/essays?page=' + page + '&size=' + size;
        var that = this;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            // console.log(res)
            if (res.code == 1) {
                for (var i = 0; i < res.data.content.length; i++) {
                    res.data.content[i].pubdate = toData(res.data.content[i].pubdate,1)
                    this.data.list.push(res.data.content[i])
                }
                this.setData({
                    list: this.data.list,
                    hiddenLoading: true
                })
            }
        }).catch(err => {})
    },
   
    // 跳转到文章详情
    goDetails (e) {
        // console.log(e.currentTarget.dataset.id)
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../details/details?id='+id,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
})